//
//  HSLoginVC.m
//  HolyShit
//
//  Created by Robin on 2017/3/30.
//  Copyright © 2017年 HolyShit. All rights reserved.
//

#import "HSLoginVC.h"
#import "HSRegisterVC.h"

@interface HSLoginVC ()

@property (nonatomic, strong) UITextField *accountTF;
@property (nonatomic, strong) UITextField *pswTF;
@property (nonatomic, strong) UIButton *confirmBtn;
@property (nonatomic, strong) UIButton *registerBtn;

@end

@implementation HSLoginVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"登录";
    
    self.accountTF = [self fieldWithFrame:CGRectMake(30, 100, ScreenWidth - 60, 50) title:@"账号" placeholder:@"请输入您的账号"];
    self.accountTF.keyboardType = UIKeyboardTypePhonePad;
    [self.view addSubview:self.accountTF];
    self.pswTF = [self fieldWithFrame:CGRectMake(self.accountTF.left, self.accountTF.bottom + 25, self.accountTF.width, self.accountTF.height) title:@"密码" placeholder:@"请输入密码"];
    self.pswTF.secureTextEntry = YES;
    [self.view addSubview:self.pswTF];
    self.confirmBtn = [UIButton buttonWithFrame:CGRectMake(self.accountTF.left, self.pswTF.bottom + 50, self.accountTF.width, 40) font:MYFont(Font32) color:WHITE_COLOR title:@"登录"];
    self.confirmBtn.layer.cornerRadius = 10;
    self.confirmBtn.backgroundColor = HEXCOLOR(HSColorBlue);
    [self.confirmBtn addTarget:self action:@selector(loginClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.confirmBtn];
    self.registerBtn = [UIButton buttonWithFrame:CGRectMake(self.confirmBtn.left, self.confirmBtn.bottom + 20, self.confirmBtn.width, 40) font:MYFont(Font32) color:WHITE_COLOR title:@"注册"];
    self.registerBtn.layer.cornerRadius = 10;
    self.registerBtn.backgroundColor = HEXCOLOR(HSColorRed);
    [self.registerBtn addTarget:self action:@selector(registerClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.registerBtn];
}

- (UITextField *)fieldWithFrame:(CGRect)frame title:(NSString *)title placeholder:(NSString *)placeholder
{
    UITextField *field = [[UITextField alloc] initWithFrame:frame];
    field.font = MYFont(Font30);
    field.textColor = HEXCOLOR(HSColorBlack);
    field.placeholder = placeholder;
    UILabel *left = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 80, field.height)];
    left.text = title;
    left.font = MYFont(Font32);
//    left.backgroundColor = BLACK_COLOR;
    left.textColor = HEXCOLOR(HSColorGray);
    field.leftView = left;
    field.leftViewMode = UITextFieldViewModeAlways;
    [field showLineForDirection:MYDirectionBottom];
    return field;
}

- (void)loginClick
{
    NSString *account = self.accountTF.text;
    if (account.length < 1) {
        [UIWindow showFailedMsg:@"账号不能为空"];
        return;
    }
    if(account.length < 8){
        [UIWindow showFailedMsg:@"账号长度必须大于8位"];
        return;
    }
    
    NSString *psw = self.pswTF.text;
    if (psw.length < 1) {
        [UIWindow showFailedMsg:@"密码不能为空"];
        return;
    }
    if(psw.length < 6){
        [UIWindow showFailedMsg:@"密码长度至少6位"];
        return;
    }
    if (![MYAppHelper shareHelper].hasNetWork) {
        [UIWindow showFailedMsg:@"网络错误"];
        return;
    }
    [UIWindow showProgress];
    [[GCDQueue mainQueue] queueBlock:^{
        if ([account isEqualToString:@"13012345678"] && [psw isEqualToString:@"123456"]) {
            [UIWindow showSuccessMsg:@"登录成功"];
            [[NSUserDefaults standardUserDefaults] setObject:account forKey:userNameKey];
            [self my_pop:YES];
        }
        else{
            [UIWindow showFailedMsg:@"账号或者密码有误"];
        }
    } afterDelay:2];
}

- (void)registerClick
{
    [self my_push:[HSRegisterVC new]];
}


@end
